﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace MailProcessing
{
    public class FTPController
    {
        private FTPSettings _ftpSettings;
        public FTPController(FTPSettings ftpSettings) 
        {
            _ftpSettings = ftpSettings;
        }
        public bool FtpUploadFile(string strToUpload)
        {
            bool uploadSuccess = false;
            if (!_ftpSettings.IsFolderExist)
            {
                if (MakeSureftpDirectoryExist(_ftpSettings.FtpFolderPath))
                {
                    _ftpSettings.IsFolderExist = true;
                }
                else
                    return false;
            }
            FtpWebResponse response = null;
            FtpWebRequest webRequest = null;
            if (_ftpSettings.FtpFolderPath[_ftpSettings.FtpFolderPath.Length - 1].Equals('/'))
            {
                webRequest = (FtpWebRequest)WebRequest.Create(string.Format("{0}{1}", _ftpSettings.FtpFolderPath, _ftpSettings.FileName));
            }
            else
            {
                webRequest = (FtpWebRequest)WebRequest.Create(string.Format("{0}/{1}", _ftpSettings.FtpFolderPath, _ftpSettings.FileName));
            }
            
            webRequest.Credentials = new NetworkCredential(_ftpSettings.UserName, _ftpSettings.Password);
            webRequest.Method = WebRequestMethods.Ftp.UploadFile;
            webRequest.UseBinary = true;
            Stream ftpstream = webRequest.GetRequestStream();
            byte[] fileContents = Encoding.UTF8.GetBytes(strToUpload);
            webRequest.ContentLength = fileContents.Length;
            ftpstream.Write(fileContents, 0, fileContents.Length);
            ftpstream.Close();
            response = (FtpWebResponse)webRequest.GetResponse();
            if (response.StatusCode == FtpStatusCode.ClosingData)
            {
                uploadSuccess = true;                
            } 
            response.Close();            
            return uploadSuccess;
        }
        private bool MakeSureftpDirectoryExist(string directory)
        {

            try
            {
                //create the directory
                FtpWebRequest requestDir = (FtpWebRequest)FtpWebRequest.Create(new Uri(directory));
                requestDir.Method = WebRequestMethods.Ftp.MakeDirectory;
                requestDir.Credentials = new NetworkCredential(_ftpSettings.UserName, _ftpSettings.Password);
                requestDir.UsePassive = true;
                requestDir.UseBinary = true;
                requestDir.KeepAlive = false;
                FtpWebResponse response = (FtpWebResponse)requestDir.GetResponse();
                Stream ftpStream = response.GetResponseStream();

                ftpStream.Close();
                response.Close();

                return true;
            }
            catch (WebException ex)
            {
                FtpWebResponse response = (FtpWebResponse)ex.Response;
                if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
                {
                    response.Close();
                    return true;
                }
                else
                {
                    response.Close();
                    return false;
                }
            }
        }
        public bool FtpCreateDirectory(string directoryPath) 
        {
            FtpWebResponse response = null;
            bool isCreateSuccess = true;
            try
            {
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(directoryPath);
                request.Credentials = new NetworkCredential(_ftpSettings.UserName, _ftpSettings.Password);
                request.Method = WebRequestMethods.Ftp.PrintWorkingDirectory;

                response = (FtpWebResponse)request.GetResponse();
                
            }
            catch (WebException ex)
            {
                isCreateSuccess = false;
            }
            finally 
            {
                if (response != null)
                    response.Close();
            }

            return isCreateSuccess;
        }
        public bool FtpDirectoryExists(string directoryPath)
        {
            bool IsExists = true;
            FtpWebResponse response = null;
            try
            {
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(directoryPath);
                request.Credentials = new NetworkCredential(_ftpSettings.UserName, _ftpSettings.Password);
                request.Method = WebRequestMethods.Ftp.PrintWorkingDirectory;

                response = (FtpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                IsExists = false;
                DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
            }
            finally
            {
                if (response != null)
                    response.Close();
            }
            return IsExists;
        }
    }
}
